   package com.javalong.affairs.module.schedule.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.Schedule;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.module.notifything.activity.EditContentActivity;
import com.javalong.affairs.base.ui.TextSelectView;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.ui.TopTitle.OnRightDrawableClick;
import com.javalong.affairs.base.utils.DateTimePickDialogUtil;
import com.javalong.affairs.base.utils.DateUtils;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_updateschedule)
public class UpdateScheduleActivity extends UIHandlerActivity implements
		OnRightDrawableClick {

	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.tv_time)
	private TextView tv_time;
	@ViewInject(R.id.et_address)
	private EditText et_address;
	@ViewInject(R.id.et_content)
	private EditText et_content;
	@ViewInject(R.id.bt_send)
	private Button bt_send;
	@ViewInject(R.id.bt_delete)
	private Button bt_delete;
	@ViewInject(R.id.ui_tsview_receiver)
	private TextSelectView tsView_receiver;
	@ViewInject(R.id.et_title)
	private EditText et_title;
	@ViewInject(R.id.bt_starttime)
	private Button bt_starttime;
	@ViewInject(R.id.bt_endtime)
	private Button bt_endtime;
	@ViewInject(R.id.tv_starttime)
	private TextView tv_starttime;
	@ViewInject(R.id.tv_endtime)
	private TextView tv_endtime;
	private Date nowDate = new Date();
	private DateTimePickDialogUtil startDialog;
	private DateTimePickDialogUtil endDialog;
	private String simpleMsgId;
	private PopupWindow pwMyPopWindow;
	private ArrayList<Map<String, String>> moreList;
	private ListView lvPopupList;
	private boolean nodelete;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		Bundle bundle = getIntent().getBundleExtra(BundleKey.BUNDLE);
		simpleMsgId = bundle.getString(BundleKey.SIMPLEMSGID);
		nodelete = bundle.getBoolean(BundleKey.NODELETE);
		if (nodelete) {
			bt_delete.setVisibility(View.GONE);
		}
		initData();
		initPopData();
		initPopupWindow();
	}

	private void initPopData() {

		moreList = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		map = new HashMap<String, String>();
		map.put("share_key", "完成日程");
		moreList.add(map);
	}

	@Override
	public void rightClick() {

		if (pwMyPopWindow.isShowing()) {

		} else {
			pwMyPopWindow.showAsDropDown(topTitle.getIv_right_drawable());
		}
		// IntentManager.doActivity(this, SendMsgActivity.class);
	}

	private void initPopupWindow() {
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.popwindow_task, null);
		lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
		pwMyPopWindow = new PopupWindow(layout);
		pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件
		lvPopupList.setAdapter(new SimpleAdapter(this, moreList,
				R.layout.item_popwindow, new String[] { "share_key" },
				new int[] { R.id.tv_list_item }));
		lvPopupList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					String url = UrlUtils.getUrl("Journal", "finish");
					Map<String, Object> params = new HashMap<String, Object>();
					params.put(RequestKey.ID, simpleMsgId);
					HttpUtils.post(url, params, new BaseResponseListener(null) {
						@Override
						public void onSuccess(Object obj) {
							super.onSuccess(obj);
							UpdateScheduleActivity.this.finish();
						}
					});
					break;
				}
			}
		});
		// 控制popupwindow的宽度和高度自适应
		lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
				View.MeasureSpec.UNSPECIFIED);
		pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
		pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight()));
		// 控制popupwindow点击屏幕其他地方消失
		pwMyPopWindow.setBackgroundDrawable(new BitmapDrawable());
		pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
		pwMyPopWindow.setFocusable(false);
	}

	private void initData() {
		String url = UrlUtils.getUrl("Journal", "outOne_journal");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsgId);
		HttpUtils.post(url, params,
				new BaseResponseListener(DataType.SCHEDULE) {
					@Override
					public void onSuccess(Object obj) {
						super.onSuccess(obj);
						initUI((Schedule) obj);
					}
				});
	}

	private Schedule schedule;

	private void initUI(Schedule schedule) {
		this.schedule = schedule;
		topTitle.setOnLeftDrawableClick(this);
		topTitle.setOnRightDrawableClick(this);
		bt_send.setOnClickListener(this);
		bt_delete.setOnClickListener(this);
		bt_starttime.setOnClickListener(this);
		bt_endtime.setOnClickListener(this);
		tv_starttime.setText(TimeUtils.getFormatTime2(schedule.timeBegin));
		tv_endtime.setText(TimeUtils.getFormatTime2(schedule.timeEnd));
		startDialog = new DateTimePickDialogUtil(this,
				DateUtils.formatDate(nowDate));
		endDialog = new DateTimePickDialogUtil(this,
				DateUtils.formatDate(nowDate));
		et_title.setText(schedule.title);
		et_content.setText(schedule.content);
		et_address.setText(schedule.place);

		User user = (User) AllData.GetInstance().get(
				R.string.data_user_current_user);
		if (user.id != schedule.userId) {
			bt_send.setVisibility(View.GONE);
		}
		et_content.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bt_starttime:
			startDialog.dateTimePicKDialog(tv_starttime);
			break;
		case R.id.bt_endtime:
			endDialog.dateTimePicKDialog(tv_endtime);
			break;
		case R.id.bt_send:
			String url = UrlUtils.getUrl("Journal", "update");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(RequestKey.ID, simpleMsgId);
			params.put(RequestKey.TITLE, et_title.getText().toString());
			params.put(RequestKey.PLACE, et_address.getText().toString());
			params.put(RequestKey.CONTENT, et_content.getText().toString());
			if (startDialog.getSeconds() != 0) {
				params.put(RequestKey.TIMEBEGIN, startDialog.getSeconds());
			} else {
				params.put(RequestKey.TIMEBEGIN, schedule.timeBegin);
			}
			if (endDialog.getSeconds() != 0) {
				params.put(RequestKey.TIMEEND, endDialog.getSeconds());
			} else {
				params.put(RequestKey.TIMEEND, schedule.timeEnd);
			}
			// params.put(RequestKey.ATTACHMENT,
			// et_timelimit.getText().toString());

			HttpUtils.post(url, params, new BaseResponseListener(null) {
				@Override
				public void onSuccess(Object obj) {
					super.onSuccess(obj);
					UpdateScheduleActivity.this.finish();
				}
			});
			break;
		case R.id.bt_delete:
			delete();
			break;
		case R.id.et_content:
			Bundle bundle = new Bundle();
			bundle.putString(BundleKey.EDITCONTENT, et_content.getText()
					.toString());
			IntentManager.doActivityForResult(this, EditContentActivity.class,
					RequestCode.EDITCONTENT, bundle);
			break;
		default:
			break;
		}
	}

	private void delete() {
		String url = UrlUtils.getUrl("Journal", "delete");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsgId);
		HttpUtils.post(url, params, new BaseResponseListener(null) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				ToastUtils.showMessage("删除成功");
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case RequestCode.EDITCONTENT:
				String editContent = data.getStringExtra(BundleKey.EDITCONTENT);
				et_content.setText(editContent);
				break;
			}
		}
	}
}
